package com.szholly.data.common.util;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.Field;
import com.szholly.data.general.IDomain;
import com.szholly.data.general.ITable;
import com.szholly.data.general.RowBase;

/**
 * 类功能说明 定义添加转义字段转换类
 */
public class ConvertCaptionUtils {
	
	private static Logger logger = Logger.getLogger(ConvertCaptionUtils.class);

	public final static String FIELD_CAPTION_NAME = "_DESC";
	
	public final static String FIELD_UNIT_NAME = "_UNIT";

	/**
	 * 获取当前查询的需要翻译的字段
	 * 
	 * @param metadataTable
	 * @param queryFilter
	 * @return
	 */
	private static HashMap<String, String> getDescField(ITable metadataTable,
			String selectFields) {
		HashMap<String, String> descFields = new HashMap<String, String>();
		if (StringUtils.IsNullOrSpace(selectFields)
				|| selectFields.trim().equals("*")) {
			selectFields = metadataTable.getSelectFields(false);
		}

		String[] fieldList = selectFields.split(",");
		for (String field : fieldList) {
			field = field.replace("[", "").replace("]", "");
			Field fm = metadataTable.getField(field);
			if (fm == null)
				continue;
			if (fm.getHasDomain()) {
				descFields.put(fm.getFieldName(), fm.getFieldName().trim()
						+ FIELD_CAPTION_NAME);
			}
		}
		return descFields;
	}

	private static void convertOnlyCode(RowBase entity, Field fm,
			String code) {
		IDomain pDomain = fm.getDomain();
		if (pDomain == null) {
			return;
		}
		
		HashMap<String, String[]> codeValues = pDomain.getDomainsEx();
		String[] codeStringList = code.split(",");
		boolean find = false;
		String caption = "";
		String unti = "";
		
		for(String codeString : codeStringList){
			if (codeValues != null && codeValues.containsKey(codeString)) {
				find = true;
				String[] value = codeValues.get(codeString);
				caption += value[0] + ",";
				if(!StringUtils.IsNullOrSpace(pDomain.getUnitFieldName())){
					unti += value[1] + ",";
				}
			}
		}
		
		if(!find){
			entity.setValue(fm.getFieldName() + FIELD_CAPTION_NAME, code);
			if(!StringUtils.IsNullOrSpace(pDomain.getUnitFieldName())){
				entity.setValue(fm.getFieldName() + FIELD_UNIT_NAME, "");
			}
		}else{
			caption = caption.substring(0, caption.length()-1);
			entity.setValue(fm.getFieldName() + FIELD_CAPTION_NAME, caption);
			
			if(!StringUtils.IsNullOrSpace(pDomain.getUnitFieldName())){
				unti = unti.substring(0, unti.length()-1);
				entity.setValue(fm.getFieldName() + FIELD_UNIT_NAME, unti);
			}
		}
	}

	@SuppressWarnings("unused")
	private static void convertNoOnlyCode(ITable metadataTable, RowBase entity,
			Field fm, String codeString) {
		Field[] pField = metadataTable.getRPFields(fm);
		if (pField == null || pField.length <= 0) {
			convertOnlyCode(entity, fm, codeString);
			return;
		}

		String rCodes = "";
		for (Field item : pField) {
			Object obj = entity.getValue(item.getDataType(),
					item.getFieldName());
			rCodes += obj.toString();
		}
		rCodes += codeString;
		convertOnlyCode(entity, fm, rCodes);
	}

	/**
	 * 转换翻译字段
	 * 
	 * @param tableMetadata
	 * @param entity
	 */
	public static void convertCaptionField(ITable tableMetadata, RowBase entity) {
		if (entity == null || tableMetadata == null)
			return;

		for (Field fm : tableMetadata.getFields()) {
			if (fm == null)
				continue;
			Object obj = entity.getValue(fm.getDataType(), fm.getFieldName());
			if (obj == null)
				continue;
			String codeString = obj.toString();
			convertOnlyCode(entity, fm, codeString);
		}
	}

	/**
	 * 处理和当前表不是一个数据库的字典转换
	 * 
	 * @param outField
	 * @param entity
	 * @param tableMetadata
	 */
	public static void convertCaptionField(ITable tableMetadata,
			String selectFields, RowBase entity) {
		if (entity == null)
			return;

		HashMap<String, String> outField = getDescField(tableMetadata,
				selectFields);
		Set<String> keys = outField.keySet();
		for (String key : keys) {
			Field fm = tableMetadata.getField(key);
			if (fm == null)
				continue;
			Object obj = entity.getValue(fm.getDataType(), key);
			if (obj == null)
				continue;
			String codeString = obj.toString();
			convertOnlyCode(entity, fm, codeString);
		}
	}

	/**
	 * 处理和当前表不是一个数据库的字典转换
	 * 
	 * @param outField
	 * @param dt
	 * @param tableMetadata
	 */
	public static <T extends RowBase> void convertCaptionField(
			ITable tableMetadata, String selectFields, List<T> dt) {
		if (dt == null || dt.size() <= 0)
			return;

		HashMap<String, String> outField = getDescField(tableMetadata,
				selectFields);
		Set<String> keys = outField.keySet();
		// 字段
		Date startDate = new Date();
		Long startTime = startDate.getTime();
		for (String key : keys) {
			Field fm = tableMetadata.getField(key);
			if (fm == null)
				continue;

			for (RowBase dr : dt) {
				RowBase entity = (RowBase) dr;
				if (null == entity)
					continue;
				Object obj = entity.getValue(fm.getDataType(), key);
				if (obj == null)
					continue;
				String codeString = obj.toString();
				convertOnlyCode(entity, fm, codeString);
			}
		}

		Date endDate = new Date();
		Long endTime = endDate.getTime();
		logger.info("【convertCaptionField】 字典翻译时间(s)" + (endTime - startTime) + "共" + keys.size()
						+ "个字段，" + dt.size() + "条记录");
	}
}